package com.jeeplus.itemInterface.storeInterface;

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.CacheUtils;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.address.service.AddressService;
import com.jeeplus.modules.centerposition.entity.CenterPosition;
import com.jeeplus.modules.centerposition.service.CenterPositionService;
import com.jeeplus.modules.express.entity.Express;
import com.jeeplus.modules.express.service.ExpressService;
import com.jeeplus.modules.goods.entity.Goods;
import com.jeeplus.modules.goods.service.GoodsService;
import com.jeeplus.modules.goodsspecs.entity.GoodsSpec;
import com.jeeplus.modules.goodsspecs.service.GoodsSpecService;
import com.jeeplus.modules.goodsstore.entity.GoodsStore;
import com.jeeplus.modules.goodsstore.service.GoodsStoreService;
import com.jeeplus.modules.member.entity.Member;
import com.jeeplus.modules.member.service.MemberService;
import com.jeeplus.modules.orders.entity.Orders;
import com.jeeplus.modules.ordersstore.entity.OrdersStore;
import com.jeeplus.modules.ordersstore.service.OrdersStoreService;
import com.jeeplus.modules.replenishment.entity.Replenishment;
import com.jeeplus.modules.replenishment.service.ReplenishmentService;
import com.jeeplus.modules.rewardparameters.entity.RewardParameters;
import com.jeeplus.modules.rewardparameters.service.RewardParametersService;
import com.jeeplus.modules.subgoods.entity.SubGoods;
import com.jeeplus.modules.subgoods.service.SubGoodsService;
import com.jeeplus.modules.zone.entity.Zone;
import com.jeeplus.modules.zone.service.ZoneService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.persistence.Convert;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Api(value = "StoreInterfaceController", description = "商城信息")
@Controller
@RequestMapping(value = "${frontPath}/store")
public class StoreInterfaceController {

	@Autowired
	private MemberService memberService;
	@Autowired
	private GoodsStoreService goodsStoreService;
	@Autowired
	private ZoneService zoneService;
	@Autowired
	private OrdersStoreService ordersStoreService;
	@Autowired
	private GoodsSpecService goodsSpecService;
	@Autowired
	private ExpressService expressService;
	@Autowired
	private SubGoodsService subGoodsService;
	@Autowired
	private CenterPositionService centerPositionService;
	@Autowired
	private ReplenishmentService replenishmentService;
	@Autowired
	private RewardParametersService rewardParametersService;


	private org.slf4j.Logger logger = LoggerFactory.getLogger(getClass());


	@ApiOperation(value = "获取所有商品",notes = "获取所有商品")
	@GetMapping("/getStoreGoodsList")
	@ResponseBody
	public AjaxJson getStoreGoodsList() throws ParseException{
		AjaxJson aj = new AjaxJson();
		@SuppressWarnings("unchecked")
		List<GoodsStore> goodsStoreList = (List<GoodsStore>) CacheUtils.get("goodsStoreList");
		if (goodsStoreList !=null && goodsStoreList.size()>0){
			for (int i = 0; i <goodsStoreList.size() ; i++) {
				String[] firstImg=goodsStoreList.get(i).getMainImgs().split("\\|");
				goodsStoreList.get(i).setFirstImg(firstImg[0]);
			}
			aj.setSuccess(true);
			aj.put("list",goodsStoreList);
		}else {
			GoodsStore goodsStore=new GoodsStore();
			goodsStore.setGoodsStatus("1");
			List<GoodsStore> list = goodsStoreService.findList(goodsStore);
			CacheUtils.put("goodsStoreList",list);
			if (list.size()>0){
				for (int i = 0; i <list.size() ; i++) {
					String[] firstImg=list.get(i).getMainImgs().split("\\|");
					list.get(i).setFirstImg(firstImg[0]);
				}
			}
			aj.setSuccess(true);
			aj.put("list",list);
		}
		return aj;
	}



	@ApiOperation(value = "根据分区获取商品",notes = "根据分区获取商品")
	@ApiImplicitParams({
			@ApiImplicitParam(value = "商品分区id",name = "zoneId",required = false,dataType = "String",paramType = "query")
	})
	@GetMapping("/getStoreGoodsZoneList")
	@ResponseBody
	public AjaxJson getStoreGoodsZoneList(String zoneId) throws ParseException{
		AjaxJson aj = new AjaxJson();
		Zone zone = zoneService.get(zoneId);
		if (zone!=null){
			GoodsStore goodsStore=new GoodsStore();
			goodsStore.setZone(zone);
			goodsStore.setGoodsStatus("1");
			List<GoodsStore> goodsList = goodsStoreService.findList(goodsStore);
			if(goodsList.size()>0){
				for (int i = 0; i <goodsList.size() ; i++) {
					String[] firstImg=goodsList.get(i).getMainImgs().split("\\|");
					goodsList.get(i).setFirstImg(firstImg[0]);
					//判断显示金额还是积分
					if (goodsList.get(i).getPrice() > 0){
						goodsList.get(i).setShowPrice("1");
					}
					if (goodsList.get(i).getIntegral() > 0){
						goodsList.get(i).setShowPrice("0");
					}
				}
			}
			aj.setSuccess(true);
			aj.put("list",goodsList);
			aj.setMsg("操作成功");
		}else {
			aj.setSuccess(false);
			aj.setMsg("该分区不存在");
		}
		return aj;
	}

	@ApiOperation(value = "获取首页时令商品",notes = "获取首页时令商品")
	@GetMapping("/getSeasonalGoods")
	@ResponseBody
	public AjaxJson getSeasonalGoods() {
		AjaxJson aj = new AjaxJson();
		List<GoodsStore> goodsList = goodsStoreService.findSeasonalList();
		if(goodsList.size()>0){
			for (int i = 0; i <goodsList.size() ; i++) {
				String[] firstImg=goodsList.get(i).getMainImgs().split("\\|");
				goodsList.get(i).setFirstImg(firstImg[0]);
				//判断显示金额还是积分
				if (goodsList.get(i).getPrice() > 0){
					goodsList.get(i).setShowPrice("1");
				}
				if (goodsList.get(i).getIntegral() > 0){
					goodsList.get(i).setShowPrice("0");
				}
			}
		}
		aj.setSuccess(true);
		aj.put("list",goodsList);
		return aj;
	}


	@ApiOperation(value = "获取分区列表",notes = "获取分区列表")
	@GetMapping("/getZoneList")
	@ResponseBody
	public AjaxJson getZoneList() throws ParseException{
		AjaxJson aj = new AjaxJson();
		Zone z=new Zone();
		List<Zone> list = zoneService.findList(z);
		if (list.size()>0){
			aj.put("list",list);
			aj.setSuccess(true);
		}else {
			aj.setSuccess(false);
			aj.setMsg("没有分区");
		}
		return aj;
	}


	@ApiOperation(value = "商城商品详情",notes = "商城商品详情")
	@ApiImplicitParams({
			@ApiImplicitParam(value = "商品ID",name = "goodsId",required = true,dataType = "String",paramType = "query")
	})
	@GetMapping("/getStoreGoodsDetail")
	@ResponseBody
	public AjaxJson getStoreGoodsDetail(String goodsId){
		AjaxJson aj = new AjaxJson();
		@SuppressWarnings("unchecked")
		GoodsStore goodsStore = (GoodsStore) CacheUtils.get(goodsId);
		if (goodsStore !=null){
			if (goodsStore.getPrice() > 0){
				goodsStore.setShowPrice("1");
			}
			if (goodsStore.getIntegral() > 0){
				goodsStore.setShowPrice("0");
			}
			aj.setSuccess(true);
			aj.setMsg("操作成功！");
			aj.put("goodsStore",goodsStore);
		}else {
			GoodsStore goodsStore1 = goodsStoreService.get(goodsId);
			if (goodsStore1 == null){
				aj.setSuccess(false);
				aj.setMsg("商品信息有误");
			}else {
				if (goodsStore1.getPrice() > 0){
					goodsStore1.setShowPrice("1");
				}
				if (goodsStore1.getIntegral() > 0){
					goodsStore1.setShowPrice("0");
				}
				CacheUtils.put(goodsId,goodsStore1);
				aj.setSuccess(true);
				aj.setMsg("操作成功！");
				aj.put("goodsStore",goodsStore1);
			}
		}
		return aj;
	}


	@ApiOperation(value = "获取规格",notes = "获取规格")
	@ApiImplicitParams({
			@ApiImplicitParam(value = "商品ID",name = "goodsId",required = true,dataType = "String",paramType = "query")
	})
	@GetMapping("/getStoreGoodsSpecs")
	@ResponseBody
	public AjaxJson getStoreGoodsSpecs(String goodsId){
		AjaxJson aj = new AjaxJson();
		GoodsStore goodsStore = goodsStoreService.get(goodsId);
		if (goodsStore == null){
			aj.setSuccess(false);
			aj.setMsg("商品信息有误");
		}else {
			GoodsSpec goodsSpec = new GoodsSpec();
			goodsSpec.setGoodsStore(goodsStore);
			goodsSpec.setSpecStatus("1");
			List<GoodsSpec> specList = goodsSpecService.findList(goodsSpec);
			aj.setSuccess(true);
			aj.setMsg("操作成功！");
			aj.put("specList",specList);
		}
		return aj;
	}



	@ApiOperation(value = "获取积分中心用户",notes = "获取积分中心用户")
	@ApiImplicitParams({
			@ApiImplicitParam(value = "用户ID",name = "memberId",required = true,dataType = "String",paramType = "query")
	})
	@GetMapping("/getIntegralCenter")
	@ResponseBody
	public AjaxJson getIntegralCenter(String memberId){
		AjaxJson aj = new AjaxJson();
		List<Member> mList = new ArrayList<>();
		Member member = new Member();
		member.setIsIntegral("1");
		List<Member> list = memberService.findList(member);
		for (Member member1 : list) {
			if (!member1.getId().equals(memberId)){
				mList.add(member1);
			}
		}
		aj.put("list",mList);
		aj.setSuccess(true);
		return aj;
	}

	@ApiOperation(value = "确认订单",notes = "确认订单")
	@ApiImplicitParams({
			@ApiImplicitParam(value = "用户id",name = "memberId",required = true,dataType = "String",paramType = "query"),
			@ApiImplicitParam(value = "商品id",name = "goodsId",required = true,dataType = "String",paramType = "query"),
			@ApiImplicitParam(value = "商品数量",name = "goodsNum",required = true,dataType = "Integer",paramType = "query"),
//			@ApiImplicitParam(value = "规格id",name = "specId",required = false,dataType = "String",paramType = "query"),

	})
	@PostMapping("/conOrder")
	@ResponseBody
	public AjaxJson conOrder(String memberId,String goodsId,Integer goodsNum){
		AjaxJson aj = new AjaxJson();
		//查询用户信息
		Member member = memberService.get(memberId);
		if (member == null){
			aj.setSuccess(false);
			aj.setMsg("用户信息有误");
		}else {
			//查询购买商品信息
			GoodsStore boughtGood = goodsStoreService.get(goodsId);
			if (boughtGood != null){
				aj = ordersStoreService.conOrder(member,boughtGood,goodsNum);
			}else {
				aj.setSuccess(false);
				aj.setMsg("商品信息有误");
			}
		}
		return aj;
	}


	@ApiOperation(value = "提交订单",notes = "提交订单")
	@ApiImplicitParams({
			@ApiImplicitParam(value = "用户id",name = "memberId",required = true,dataType = "String",paramType = "query"),
			@ApiImplicitParam(value = "商品id",name = "goodsId",required = true,dataType = "String",paramType = "query"),
			@ApiImplicitParam(value = "商品数量",name = "goodsNum",required = true,dataType = "Integer",paramType = "query"),
//			@ApiImplicitParam(value = "规格id",name = "specId",required = false,dataType = "String",paramType = "query"),
			@ApiImplicitParam(value = "地址id",name = "addressId",required = true,dataType = "String",paramType = "query"),
			@ApiImplicitParam(value = "积分中心",name = "serviceId",required = true,dataType = "String",paramType = "query"),
	})
	@PostMapping("/placeOrder")
	@ResponseBody
	public AjaxJson placeOrder(String memberId,String goodsId,Integer goodsNum,String addressId,String serviceId){
		AjaxJson aj = new AjaxJson();
		//查询用户信息
		Member member = memberService.get(memberId);
		if (member == null){
			aj.setSuccess(false);
			aj.setMsg("用户信息有误");
		}else {
//			aj.setSuccess(false);
//			aj.setMsg("暂时关闭");
			//查询购买商品信息
			GoodsStore boughtGood = goodsStoreService.get(goodsId);
			aj = ordersStoreService.placeOrder(member,boughtGood,goodsNum,addressId,serviceId);
		}
		return aj;
	}



	@ApiOperation(value = "订单支付",notes = "订单支付")
	@ApiImplicitParams({
			@ApiImplicitParam(value = "用户id",name = "memberId",required = true,dataType = "String",paramType = "query"),
			@ApiImplicitParam(value = "订单编号",name = "orderNo",required = true,dataType = "String",paramType = "query")
	})
	@PostMapping("/payOrder")
	@ResponseBody
	public AjaxJson payOrder(String memberId, String orderNo){
		AjaxJson aj = new AjaxJson();
		//查询用户信息
		Member member = memberService.get(memberId);
		OrdersStore ordersStore = ordersStoreService.getByNo(orderNo);
		if (member == null){
			aj.setSuccess(false);
			aj.setMsg("用户信息有误");
		}else {
			if (ordersStore!=null) {
				aj = ordersStoreService.payOrder(member,ordersStore);
			}else {
				aj.setSuccess(false);
				aj.setMsg("订单信息有误");
			}
		}
		return aj;
	}




	@ApiOperation(value = "获取订单列表",notes = "获取订单列表")
	@ApiImplicitParams({
			@ApiImplicitParam(value = "用户id",name = "memberId",required = true,dataType = "String",paramType = "query"),
			@ApiImplicitParam(value = "订单类型（0:全部订单1:未支付2:未发货3:已发货4:已完成5：已取消）",name = "orderType",required = true,dataType = "String",paramType = "query")
	})
	@GetMapping("/getOrdersList")
	@ResponseBody
	public AjaxJson getOrdersList(String memberId,String orderType){
		AjaxJson aj = new AjaxJson();

		//查询用户信息
		Member member = memberService.get(memberId);
		if (member == null){
			aj.setSuccess(false);
			aj.setMsg("用户信息有误");
			return aj;
		}else {
			//未支付
			if ("1".equals(orderType)){
				OrdersStore ordersStore = new OrdersStore();
				ordersStore.setOrdersStatus("1");
				ordersStore.setMember(member);
				List<OrdersStore> ordersList = ordersStoreService.findLimitList(ordersStore);
				if (ordersList.size() > 0){
					for (OrdersStore store : ordersList) {
						if (store.getMoney() != null){
							if (store.getMoney() > 0){
								store.setShowPrice("1");
							}
						}
						if (store.getJifen() != null){
							if (store.getJifen() > 0){
								store.setShowPrice("0");
							}
						}
					}
				}
//				if (ordersList.size()>0){
					aj.put("list",ordersList);
					aj.setSuccess(true);
//				}else {
//					aj.setSuccess(false);
//					aj.setMsg("没有订单");
//				}
			}
			//未发货
			else if ("2".equals(orderType)){
				OrdersStore ordersStore = new OrdersStore();
				ordersStore.setOrdersStatus("2");
				ordersStore.setMember(member);
				List<OrdersStore> ordersList = ordersStoreService.findLimitList(ordersStore);
				if (ordersList.size() > 0){
					for (OrdersStore store : ordersList) {
						if (store.getMoney() != null){
							if (store.getMoney() > 0){
								store.setShowPrice("1");
							}
						}
						if (store.getJifen() != null){
							if (store.getJifen() > 0){
								store.setShowPrice("0");
							}
						}
					}
				}
//				if (ordersList.size()>0){
					aj.put("list",ordersList);
					aj.setSuccess(true);
//				}else {
//					aj.setSuccess(false);
//					aj.setMsg("没有订单");
//				}
			}
			//全部
			else if ("0".equals(orderType)){
				OrdersStore ordersStore = new OrdersStore();
				ordersStore.setMember(member);
				List<OrdersStore> ordersList = ordersStoreService.findLimitList(ordersStore);
				if (ordersList.size() > 0){
					for (OrdersStore store : ordersList) {
						if (store.getMoney() != null){
							if (store.getMoney() > 0){
								store.setShowPrice("1");
							}
						}
						if (store.getJifen() != null){
							if (store.getJifen() > 0){
								store.setShowPrice("0");
							}
						}
					}
				}
//				if (ordersList.size()>0){
					aj.put("list",ordersList);
					aj.setSuccess(true);
//				}else {
//					aj.setSuccess(false);
//					aj.setMsg("没有订单");
//				}
			}
			//已发货
			else if ("3".equals(orderType)){
				OrdersStore ordersStore = new OrdersStore();
				ordersStore.setOrdersStatus("3");
				ordersStore.setMember(member);
				List<OrdersStore> ordersList = ordersStoreService.findLimitList(ordersStore);
				if (ordersList.size() > 0){
					for (OrdersStore store : ordersList) {
						if (store.getMoney() != null){
							if (store.getMoney() > 0){
								store.setShowPrice("1");
							}
						}
						if (store.getJifen() != null){
							if (store.getJifen() > 0){
								store.setShowPrice("0");
							}
						}
					}
				}
//				if (ordersList.size()>0){
					aj.put("list",ordersList);
					aj.setSuccess(true);
//				}else {
//					aj.setSuccess(false);
//					aj.setMsg("没有订单");
//				}
			}
			//已完成
			else if ("4".equals(orderType)){
				OrdersStore ordersStore = new OrdersStore();
				ordersStore.setOrdersStatus("4");
				ordersStore.setMember(member);
				List<OrdersStore> ordersList = ordersStoreService.findLimitList(ordersStore);
				if (ordersList.size() > 0){
					for (OrdersStore store : ordersList) {
						if (store.getMoney() != null){
							if (store.getMoney() > 0){
								store.setShowPrice("1");
							}
						}
						if (store.getJifen() != null){
							if (store.getJifen() > 0){
								store.setShowPrice("0");
							}
						}
					}
				}
//				if (ordersList.size()>0){
					aj.put("list",ordersList);
					aj.setSuccess(true);
//				}else {
//					aj.setSuccess(false);
//					aj.setMsg("没有订单");
//				}
			}
			return aj;
		}
	}



	@ApiOperation(value = "获取订单详情",notes = "获取订单详情")
	@ApiImplicitParams({
			@ApiImplicitParam(value = "用户id",name = "memberId",required = true,dataType = "String",paramType = "query"),
			@ApiImplicitParam(value = "订单编号",name = "ordersNo",required = true,dataType = "String",paramType = "query")
	})
	@GetMapping("/getOrdersDetail")
	@ResponseBody
	public AjaxJson getOrdersDetail(String memberId,String ordersNo) {
		AjaxJson aj = new AjaxJson();
		//查询用户信息
		Member member = memberService.get(memberId);
		if (member == null) {
			aj.setSuccess(false);
			aj.setMsg("用户信息有误");
		} else {
			OrdersStore byNo = ordersStoreService.getByNo(ordersNo);
			Express express = new Express();
			express.setOrderForm(ordersNo);
			express.setMember(member);
			List<Express> list = expressService.findList(express);
			if (byNo==null){
				aj.setSuccess(false);
				aj.setMsg("订单信息有误");
			}else {
				if (byNo.getMoney() != null){
					if (byNo.getMoney() > 0){
						byNo.setShowPrice("1");
					}
				}
				if (byNo.getJifen() != null){
					if (byNo.getJifen() > 0){
						byNo.setShowPrice("0");
					}
				}
				GoodsSpec goodsSpec = goodsSpecService.get(byNo.getSpec());
				if (goodsSpec != null){
					byNo.setGoodsSpec(goodsSpec);
				}
				aj.put("order",byNo);
				aj.put("express",list.size()==1?list.get(0):"");
				aj.setSuccess(true);
			}
		}
		return aj;
	}









    @ApiOperation(value = "删除订单",notes = "删除订单")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户id",name = "memberId",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(value = "订单编号",name = "orderNo",required = true,dataType = "String",paramType = "query")
    })
    @PostMapping("/deleteOrder")
    @ResponseBody
    public AjaxJson deleteOrder(String memberId,String orderNo){
		String status="5";
        AjaxJson aj = new AjaxJson();
        //查询用户信息
        Member member = memberService.get(memberId);
        if (member == null){
            aj.setSuccess(false);
            aj.setMsg("用户信息有误");
        }else {
			OrdersStore byNo = ordersStoreService.getByNo(orderNo);
			if (byNo!=null&&status.equals(byNo.getOrdersStatus())){
				byNo.setDelFlag("1");
				ordersStoreService.save(byNo);
				aj.setMsg("操作成功");
			}else{
				aj.setSuccess(false);
				aj.setMsg("订单信息有误");
			}

		}
        return aj;
    }


	@ApiOperation(value = "确认收货",notes = "确认收货")
	@ApiImplicitParams({
			@ApiImplicitParam(value = "用户id",name = "memberId",required = true,dataType = "String",paramType = "query"),
			@ApiImplicitParam(value = "订单编号",name = "orderNo",required = true,dataType = "String",paramType = "query")
	})
	@PostMapping("/confirmReceive")
	@ResponseBody
	public AjaxJson confirmReceive(String memberId,String orderNo){
		AjaxJson aj = new AjaxJson();
		//查询用户信息
		Member member = memberService.get(memberId);
		if (member == null){
			aj.setSuccess(false);
			aj.setMsg("用户信息有误");
		}else {
			OrdersStore byNo = ordersStoreService.getByNo(orderNo);
			if (byNo!=null){
				byNo.setOrdersStatus("4");
				ordersStoreService.save(byNo);
				aj.setMsg("操作成功");
			}else{
				aj.setSuccess(false);
				aj.setMsg("订单信息有误");
			}

		}
		return aj;
	}


	@ApiOperation(value = "取消订单",notes = "取消订单")
	@ApiImplicitParams({
			@ApiImplicitParam(value = "用户id",name = "memberId",required = true,dataType = "String",paramType = "query"),
			@ApiImplicitParam(value = "订单编号",name = "orderNo",required = true,dataType = "String",paramType = "query")
	})
	@PostMapping("/cancelOrder")
	@ResponseBody
	public AjaxJson cancelOrder(String memberId,String orderNo){
		String status="1";
		AjaxJson aj = new AjaxJson();
		//查询用户信息
		Member member = memberService.get(memberId);
		if (member == null){
			aj.setSuccess(false);
			aj.setMsg("用户信息有误");
		}else {
			OrdersStore byNo = ordersStoreService.getByNo(orderNo);
			if (byNo!=null&&status.equals(byNo.getOrdersStatus())){
				byNo.setOrdersStatus("5");
				ordersStoreService.save(byNo);
				aj.setMsg("操作成功");
			}else{
				aj.setSuccess(false);
				aj.setMsg("订单信息有误");
			}

		}
		return aj;
	}



	@ApiOperation(value = "获取规格详情",notes = "获取规格详情")
	@ApiImplicitParams({
//			@ApiImplicitParam(value = "用户id",name = "memberId",required = true,dataType = "String",paramType = "query"),
			@ApiImplicitParam(value = "规格id",name = "specId",required = true,dataType = "String",paramType = "query")
	})
	@GetMapping("/getSpecDetail")
	@ResponseBody
	public AjaxJson getSpecDetail(String specId){
		AjaxJson aj = new AjaxJson();
		GoodsSpec goodsSpec = goodsSpecService.get(specId);
		if (goodsSpec != null){
			aj.setSuccess(true);
			aj.put("goodsSpec",goodsSpec);
		}else {
			aj.setSuccess(false);
			aj.setMsg("规格详情有误");
		}
		return aj;
	}


	@ApiOperation(value = "获取积分中心订单",notes = "获取积分中心订单")
	@ApiImplicitParams({
			@ApiImplicitParam(value = "用户id",name = "memberId",required = true,dataType = "String",paramType = "query"),
			@ApiImplicitParam(value = "标识（0待发货1已发货2已完成）",name = "flag",required = true,dataType = "String",paramType = "query")
	})
	@GetMapping("/getIntegralCenterOrders")
	@ResponseBody
	public AjaxJson getIntegralCenterOrders(String memberId,String flag){
		AjaxJson aj = new AjaxJson();
		//兑换中心用户
		Member member = memberService.get(memberId);
		if (member != null){
			switch (flag){
				case "0":
					OrdersStore ordersStore = new OrdersStore();
					ordersStore.setOrdersStatus("2");
					ordersStore.setSpec(memberId);
					List<OrdersStore> list = ordersStoreService.findList(ordersStore);
					aj.put("list",list);
					aj.setSuccess(true);
					return aj;
				case "1":
					OrdersStore ordersStore1 = new OrdersStore();
					ordersStore1.setOrdersStatus("3");
					ordersStore1.setSpec(memberId);
					List<OrdersStore> list1 = ordersStoreService.findList(ordersStore1);
					aj.put("list",list1);
					aj.setSuccess(true);
					return aj;
				case "2":
					OrdersStore ordersStore2 = new OrdersStore();
					ordersStore2.setOrdersStatus("4");
					ordersStore2.setSpec(memberId);
					List<OrdersStore> list2 = ordersStoreService.findList(ordersStore2);
					aj.put("list",list2);
					aj.setSuccess(true);
					return aj;
				default:
					aj.put("list","");
					aj.setSuccess(true);
					return aj;
			}
		}else {
			aj.setSuccess(false);
			aj.setMsg("用户信息有误");
		}
		return aj;
	}


	@ApiOperation(value = "进行兑换",notes = "进行兑换")
	@ApiImplicitParams({
			@ApiImplicitParam(value = "用户id",name = "memberId",required = true,dataType = "String",paramType = "query"),
			@ApiImplicitParam(value = "订单编号",name = "orderNo",required = true,dataType = "String",paramType = "query")
	})
	@PostMapping("/goToExchange")
	@ResponseBody
	public AjaxJson goToExchange(String memberId,String orderNo){
		AjaxJson aj = new AjaxJson();
		//兑换中心用户
		Member member = memberService.get(memberId);
		if (member != null){
//			aj.setSuccess(false);
//			aj.setMsg("暂时关闭");
			aj = ordersStoreService.goToExchange(member,orderNo);
		}else {
			aj.setSuccess(false);
			aj.setMsg("用户信息有误");
		}
		return aj;
	}



	@ApiOperation(value = "我要发货",notes = "我要发货")
	@ApiImplicitParams({
			@ApiImplicitParam(value = "用户id",name = "memberId",required = true,dataType = "String",paramType = "query"),
			@ApiImplicitParam(value = "订单编号",name = "orderNo",required = true,dataType = "String",paramType = "query"),
			@ApiImplicitParam(value = "快递公司",name = "expressName",required = true,dataType = "String",paramType = "query"),
			@ApiImplicitParam(value = "快递单号",name = "expressNo",required = true,dataType = "String",paramType = "query"),
	})
	@PostMapping("deliverGoods")
	@ResponseBody
	public AjaxJson deliverGoods(String memberId,String orderNo,String expressName,String expressNo){
		AjaxJson aj = new AjaxJson();
		//兑换中心用户
		Member member = memberService.get(memberId);
		if (member != null){
			aj = ordersStoreService.deliverGoods(member,orderNo,expressName,expressNo);
		}else {
			aj.setSuccess(false);
			aj.setMsg("用户信息有误");
		}
		return aj;
	}


	@ApiOperation(value = "积分订单详情",notes = "积分订单详情")
	@ApiImplicitParams({
			@ApiImplicitParam(value = "用户id",name = "memberId",required = true,dataType = "String",paramType = "query"),
			@ApiImplicitParam(value = "订单编号",name = "orderNo",required = true,dataType = "String",paramType = "query")
	})
	@GetMapping("/getIntegralCenterOrdersDetail")
	@ResponseBody
	public AjaxJson getIntegralCenterOrdersDetail(String memberId,String orderNo){
		AjaxJson aj = new AjaxJson();
		//兑换中心用户
		Member member = memberService.get(memberId);
		if (member != null){
			OrdersStore ordersStore = ordersStoreService.getByNo(orderNo);
			Express express = new Express();
			express.setOrdersstore(ordersStore);
			List<Express> list = expressService.findList(express);
			aj.setSuccess(true);
			aj.put("ordersStore",ordersStore);
			aj.put("express",list.size()>0?list.get(0):"");
		}else {
			aj.setSuccess(false);
			aj.setMsg("用户信息有误");
		}
		return aj;
	}


	@ApiOperation(value = "我的商品库存",notes = "我的商品库存")
	@ApiImplicitParams({
			@ApiImplicitParam(value = "用户id",name = "memberId",required = true,dataType = "String",paramType = "query"),
			@ApiImplicitParam(value = "名称",name = "goodsName",required = false,dataType = "String",paramType = "query"),
	})
	@GetMapping("/getAvailableGoods")
	@ResponseBody
	public AjaxJson getAvailableGoods(String memberId,String goodsName){
		AjaxJson aj = new AjaxJson();
		//兑换中心用户
		Member member = memberService.get(memberId);
		if (member != null){
			List<CenterPosition> aList = new ArrayList<>();
			GoodsStore goodsStore1 = new GoodsStore();
			goodsStore1.setName(goodsName);
			CenterPosition centerPosition = new CenterPosition();
			centerPosition.setGoodsStore(goodsStore1);
			centerPosition.setMember(member);
			List<CenterPosition> list = centerPositionService.findList(centerPosition);
			if (list.size() > 0){
				//积分中心积分收益奖励比例
				Double percentIntegral = 0.0;
				//查询奖励比例
				List<RewardParameters> rewardParametersList = rewardParametersService.findList(new RewardParameters());
				if (rewardParametersList.size()>0){
					percentIntegral = rewardParametersList.get(0).getIntePercentIntegral();
				}
				for (CenterPosition position : list) {
					GoodsStore goodsStore = goodsStoreService.get(position.getGoodsStore().getId());
					String[] firstImg=goodsStore.getMainImgs().split("\\|");
					goodsStore.setFirstImg(firstImg[0]);
					goodsStore.setProfit(goodsStore.getIntegral()*(1-percentIntegral));
					position.setGoodsStore(goodsStore);
					if (!"2".equals(goodsStore.getZone().getId())){
						aList.add(position);
					}
				}
			}
			aj.put("list",aList);
			aj.setSuccess(true);
		}else {
			aj.setSuccess(false);
			aj.setMsg("用户信息有误");
		}
		return aj;
	}

	@ApiOperation(value = "商品库存详情",notes = "商品库存详情")
	@ApiImplicitParams({
			@ApiImplicitParam(value = "用户id",name = "memberId",required = true,dataType = "String",paramType = "query"),
			@ApiImplicitParam(value = "商品id",name = "goodsId",required = true,dataType = "String",paramType = "query"),
	})
	@GetMapping("/getAvailableGoodsDetail")
	@ResponseBody
	public AjaxJson getAvailableGoodsDetail(String memberId,String goodsId){
		AjaxJson aj = new AjaxJson();
		//兑换中心用户
		Member member = memberService.get(memberId);
		if (member != null){
			GoodsStore entity = new GoodsStore();
			entity.setId(goodsId);
			CenterPosition centerPosition = new CenterPosition();
			centerPosition.setMember(member);
			centerPosition.setGoodsStore(entity);
			List<CenterPosition> list = centerPositionService.findList(centerPosition);
			if (list.size() > 0){
				//积分中心积分收益奖励比例
				Double percentIntegral = 0.0;
				//查询奖励比例
				List<RewardParameters> rewardParametersList = rewardParametersService.findList(new RewardParameters());
				if (rewardParametersList.size()>0){
					percentIntegral = rewardParametersList.get(0).getIntePercentIntegral();
				}
				for (CenterPosition position : list) {
					GoodsStore goodsStore = goodsStoreService.get(position.getGoodsStore().getId());
					String[] firstImg=goodsStore.getMainImgs().split("\\|");
					goodsStore.setFirstImg(firstImg[0]);
					goodsStore.setProfit(goodsStore.getIntegral()*(1-percentIntegral));
					position.setGoodsStore(goodsStore);
				}
			}
			aj.put("detail",list.get(0));
			aj.setSuccess(true);
		}else {
			aj.setSuccess(false);
			aj.setMsg("用户信息有误");
		}
		return aj;
	}

	@ApiOperation(value = "销售历史",notes = "销售历史")
	@ApiImplicitParams({
			@ApiImplicitParam(value = "用户id",name = "memberId",required = true,dataType = "String",paramType = "query"),
			@ApiImplicitParam(value = "商品id",name = "goodsId",required = true,dataType = "String",paramType = "query"),
	})
	@GetMapping("/getSales")
	@ResponseBody
	public AjaxJson getSales(String memberId, String goodsId){
		AjaxJson aj = new AjaxJson();
		OrdersStore ordersStore = new OrdersStore();
		GoodsStore goodsStore = new GoodsStore();
		goodsStore.setId(goodsId);
		ordersStore.setGoodsStore(goodsStore);
		ordersStore.setSpec(memberId);
		List<OrdersStore> list = ordersStoreService.findLimitList(ordersStore);
		aj.put("list",list);
		aj.setSuccess(true);
		return aj;
	}


	@ApiOperation(value = "补货商品列表",notes = "补货商品列表")
	@ApiImplicitParams({
			@ApiImplicitParam(value = "用户id",name = "memberId",required = true,dataType = "String",paramType = "query"),
			@ApiImplicitParam(value = "商品名称",name = "goodsName",required = false,dataType = "String",paramType = "query"),
	})
	@GetMapping("/getReplenishments")
	@ResponseBody
	public AjaxJson getReplenishments(String memberId ,String goodsName){
		AjaxJson aj = new AjaxJson();
		Member member = memberService.get(memberId);
		if (member != null){
			GoodsStore goodsStore=new GoodsStore();
			goodsStore.setGoodsStatus("1");
			goodsStore.setName(goodsName);
			List<GoodsStore> list = goodsStoreService.findListForRep(goodsStore);
			if (list.size()>0){
				//积分中心积分收益奖励比例
				Double percentIntegral = 0.0;
				//查询奖励比例
				List<RewardParameters> rewardParametersList = rewardParametersService.findList(new RewardParameters());
				if (rewardParametersList.size()>0){
					percentIntegral = rewardParametersList.get(0).getIntePercentIntegral();
				}
				for (int i = 0; i <list.size() ; i++) {
					String[] firstImg=list.get(i).getMainImgs().split("\\|");
					list.get(i).setFirstImg(firstImg[0]);
					list.get(i).setProfit(list.get(i).getIntegral()*(1-percentIntegral));
				}
			}
			aj.put("list",list);
		}else {
			aj.setSuccess(false);
			aj.setMsg("用户信息有误");
		}
		return aj;
	}


	@ApiOperation(value = "补货商品详情",notes = "补货商品详情")
	@ApiImplicitParams({
			@ApiImplicitParam(value = "用户id",name = "memberId",required = true,dataType = "String",paramType = "query"),
			@ApiImplicitParam(value = "商品ID",name = "goodsId",required = true,dataType = "String",paramType = "query")
	})
	@GetMapping("/getReplenishmentDetail")
	@ResponseBody
	public AjaxJson getReplenishmentDetail(String memberId,String goodsId){
		AjaxJson aj = new AjaxJson();
		Member member = memberService.get(memberId);
		if (member != null){
			GoodsStore goodsStore1 = goodsStoreService.get(goodsId);
			if (goodsStore1 == null){
				aj.setSuccess(false);
				aj.setMsg("商品信息有误");
			}else {
				//积分中心积分收益奖励比例
				Double percentIntegral = 0.0;
				//查询奖励比例
				List<RewardParameters> rewardParametersList = rewardParametersService.findList(new RewardParameters());
				if (rewardParametersList.size()>0){
					percentIntegral = rewardParametersList.get(0).getIntePercentIntegral();
				}
				String[] firstImg=goodsStore1.getMainImgs().split("\\|");
				goodsStore1.setFirstImg(firstImg[0]);
				BigDecimal profit = BigDecimal.valueOf(goodsStore1.getIntegral()*(1-percentIntegral)).setScale(2,BigDecimal.ROUND_DOWN);
				goodsStore1.setProfit(profit.doubleValue());
				CenterPosition centerPosition = new CenterPosition();
				centerPosition.setMember(member);
				centerPosition.setGoodsStore(goodsStore1);
				List<CenterPosition> list = centerPositionService.findList(centerPosition);
				if (list.size() > 0){
					goodsStore1.setMyStock(list.get(0).getStock());
				}else {
					goodsStore1.setMyStock(0);
				}

				aj.setSuccess(true);
				aj.setMsg("操作成功！");
				aj.put("goodsStore",goodsStore1);
			}
		}else {
			aj.setSuccess(false);
			aj.setMsg("用户信息有误");
		}


		return aj;
	}


	@ApiOperation(value = "申请补货",notes = "申请补货")
	@ApiImplicitParams({
			@ApiImplicitParam(value = "用户id",name = "memberId",required = true,dataType = "String",paramType = "query"),
			@ApiImplicitParam(value = "商品id",name = "goodsId",required = true,dataType = "String",paramType = "query"),
			@ApiImplicitParam(value = "数量",name = "num",required = true,dataType = "integer",paramType = "query"),
			@ApiImplicitParam(value = "联系电话",name = "mobile",required = true,dataType = "String",paramType = "query"),
			@ApiImplicitParam(value = "收货地址",name = "address",required = true,dataType = "String",paramType = "query"),
	})
	@PostMapping("/applyForReplenishment")
	@ResponseBody
	public AjaxJson applyForReplenishment(String memberId, String goodsId, Integer num, String mobile, String address){
		AjaxJson aj = new AjaxJson();
		//兑换中心用户
		Member member = memberService.get(memberId);
		if (member != null){
			GoodsStore goodsStore = goodsStoreService.get(goodsId);
			if (goodsStore != null){
				try {
					if (goodsStore.getStock() < num){
						aj.setSuccess(false);
						aj.setMsg("平台库存不足");
						return aj;
					}
					if (num <= 0){
						aj.setSuccess(false);
						aj.setMsg("请输入正确数量");
						return aj;
					}
					replenishmentService.apply(member,goodsStore,num,mobile,address);
					aj.setSuccess(true);
					aj.setMsg("申请成功");
				}catch (Exception e){
					e.printStackTrace();
					aj.setSuccess(false);
					aj.setMsg("申请失败,"+e.getMessage());
				}
			}else {
				aj.setSuccess(false);
				aj.setMsg("商品信息有误");
			}
		}else {
			aj.setSuccess(false);
			aj.setMsg("用户信息有误");
		}
		return aj;
	}


	@ApiOperation(value = "补货记录",notes = "补货记录")
	@ApiImplicitParams({
			@ApiImplicitParam(value = "用户id",name = "memberId",required = true,dataType = "String",paramType = "query"),
	})
	@GetMapping("/getReplenishmentsHistory")
	@ResponseBody
	public AjaxJson getReplenishmentsHistory(String memberId){
		AjaxJson aj = new AjaxJson();
		Member member = memberService.get(memberId);
		if (member != null){
			Replenishment replenishment = new Replenishment();
			replenishment.setMember(member);
			List<Replenishment> list = replenishmentService.findList(replenishment);
			if (list.size() > 0){
				for (Replenishment replenishment1 : list) {
					GoodsStore goodsStore = goodsStoreService.get(replenishment1.getGoodsStore().getId());
					if (goodsStore != null){
						replenishment1.setGoodsStore(goodsStore);
					}
					if ("0".equals(replenishment1.getShipped()) && "0".equals(replenishment1.getRemarks())){
						//待发货
						replenishment1.setShipped("0");
					}else if ("1".equals(replenishment1.getShipped()) && "0".equals(replenishment1.getRemarks())){
						//已发货
						replenishment1.setShipped("1");
					}else if ("0".equals(replenishment1.getShipped()) && "1".equals(replenishment1.getRemarks())){
						//已取消
						replenishment1.setShipped("2");
					}
				}
			}
			aj.put("list",list);
		}else {
			aj.setSuccess(false);
			aj.setMsg("用户信息有误");
		}
		return aj;
	}


	@ApiOperation(value = "补货记录详情",notes = "补货记录详情")
	@ApiImplicitParams({
			@ApiImplicitParam(value = "用户id",name = "memberId",required = true,dataType = "String",paramType = "query"),
			@ApiImplicitParam(value = "记录id",name = "recordId",required = true,dataType = "String",paramType = "query"),
	})
	@GetMapping("/getReplenishmentsDetail")
	@ResponseBody
	public AjaxJson getReplenishmentsDetail(String memberId, String recordId){
		AjaxJson aj = new AjaxJson();
		Member member = memberService.get(memberId);
		if (member != null){
			Replenishment replenishment = replenishmentService.get(recordId);
			if (replenishment != null){
				GoodsStore goodsStore = goodsStoreService.get(replenishment.getGoodsStore().getId());
				if (goodsStore != null){
					replenishment.setGoodsStore(goodsStore);
				}
				if ("0".equals(replenishment.getShipped()) && "0".equals(replenishment.getRemarks())){
					//待发货
					replenishment.setShipped("0");
				}else if ("1".equals(replenishment.getShipped()) && "0".equals(replenishment.getRemarks())){
					//已发货
					replenishment.setShipped("1");
				}else if ("0".equals(replenishment.getShipped()) && "1".equals(replenishment.getRemarks())){
					//已取消
					replenishment.setShipped("2");
				}
			}
			aj.put("detail",replenishment);
		}else {
			aj.setSuccess(false);
			aj.setMsg("用户信息有误");
		}
		return aj;
	}

}
